import 'package:flutter/material.dart';
import "package:get/get.dart";

import '../../../../apis/maintenance.dart';
import '../../../../components/photo_preview/fade_foute.dart';
import '../../../../components/photo_preview/photo_preview.dart';
import '../../../../components/status/toast.dart';
import '../../../../config/base.dart';
import '../../../../controller/gather_ctrl.dart';
import '../../../../controller/network/bridge.dart';
import '../../../../controller/network/network_ctrl.dart';
import '../../../../model/bean/maintenance_details_bean.dart';

// 图片类型
enum IMGTYINGS {
  BUILDINGPHOTOPATH, // 楼栋门口照片
  SHAFTPHOTOPATH, // 电梯井照片
}

class DetailsCtrl extends GatherCtrl {
  // 查看网络状态
  final _nc = Get.find<NetworkCtrl>();

  // 组件是否初始化完成
  RxBool isInitComplete = false.obs;

  // 标记符号（用于检查请求完成后无数据组件与数据组件之的切换）
  final MARKSYMBOL = false.obs;

  // 页面数据
  final detailsData = Rx<MaintenanceDetailsBean>();

  @override
  void onInit() {
    // 创建网络变化观察
    Bridget.createNetworkCtrl(this);
    //print(Get.arguments["id"]);

    // 请求界面数据
    readDetailsInfo();
  }

  @override
  void onClose() {
    // 移除观察
    Bridget.delNetworkCtrl(this);
  }

  @override
  void modify() {
    // 网络恢复正常时执行当前函数
    if (_nc.networkStatus.value) readDetailsInfo();
  }

  /// 获取详情信息
  void readDetailsInfo() async {
    MaintenanceDetailsBean result;
    Map<String, dynamic> _par = {"id": Get.arguments["id"]};

    // 加载动画
    Toast.loading();

    try {
      // 请求数据
      result = await Maintenance.queryMaintenanceDetails(_par);
      if (result != null) {
        detailsData.value = result;
        MARKSYMBOL.value = true;
      }
    } catch (e) {}

    // 标记加载完成
    Toast.closeToast();
    isInitComplete.value = true;
  }

  /// 拆分字符串
  /// * [imgUrl] String|解析的字符串|必填
  List<String> splitString(String imgUrl, {IMGTYINGS type}) {
    List<String> _ls = [], _newls = [];
    if (imgUrl != null) {
      _ls = imgUrl.split(',');
      // 遍历添加基础路径
      for (int i = 0; i < _ls.length; i++) {
        _newls.add("${BaseConfig.BASEURL.IMG['m1']}${_ls[i]}");
      }
    }

    return _newls;
  }

  /// 查看图片
  /// * [i] int|图片下标|必填
  /// * [dataSource] List|图片数据集合|必填
  void reviewImages(int i, List dataSource) {
    Navigator.of(Get.context).push(
      FadeRoute(
        page: PhotoPreview(
          images: dataSource,
          index: i,
          heroTag: dataSource[i],
        ),
      ),
    );
  }
}
